package com.woniu.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.woniu.entity.*;
import com.woniu.pojo.*;
import com.woniu.pojo.Class;
import com.woniu.service.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("score")
public class ScoreContriller {

    @Autowired
    ScoreService scoreService;
    @Autowired
    StudentService studentService;
    @Autowired
    CourseService courseService;
    @Autowired
    EmpRoleService empRoleService;
    @Autowired
    TeacherCourseService teacherCourseService;


    /**
     * 跳转到输入成绩界面
     * */
    @RequestMapping("addForm")
    public String addForm(Integer classId,Integer courseId,Model model){
        model.addAttribute("classId", classId);
        model.addAttribute("courseId", courseId);
        return "WEB-INF/score/addSore";
    }

    /**
     * 跳转学生查询成绩页面
     * */
    @RequestMapping("stuFindForm")
    public String stuFindForm(){
        return "WEB-INF/score/studentFindScore";
    }

    /**
     * 跳转老师查询成绩页面
     * */
    @RequestMapping("empFindForm")
    public String empFindForm(HttpSession session){
        User1 user=(User1)session.getAttribute("user");
        List<Emprole1> isEmpRole = empRoleService.findIsEmpRole(user.getUid());
        String empRole=null;
        for (Emprole1 e : isEmpRole) {
            if (e.getRoleId().getRoleName().equals("讲师")) {
                empRole="讲师";
            }else if (e.getRoleId().getRoleName().equals("教学主管")) {
                empRole="教学主管";
                break;
            }else if (e.getRoleId().getRoleName().equals("班主任")) {
                empRole="班主任";
            }else if (e.getRoleId().getRoleName().equals("就业专员")) {
                empRole="就业专员";
                break;
            }
        }
        session.setAttribute("empRole", empRole);

        return "WEB-INF/score/empFindScore";
    }

    /**
     * 成绩表增加学生信息
     */
    @RequestMapping("add")
    @ResponseBody
    public String add(Score1 score,HttpSession session){
        try {
            Score score1=new Score();
            score1.setStudentId(score.getStudentId().getStudentId());
            score1.setClassId(score.getClassId().getClassId());
            score1.setCourseId(score.getCourseId().getCourseId());
            User1 user=(User1)session.getAttribute("user");
            score1.setEmpId(user.getUid());
            scoreService.add(score1);
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
        return "ok";
    }

    /**
     * 跳转输入成绩前的中间界面
     * @return
     */
    @RequestMapping("formTemp")
    public String formTemp(Integer classId){
        return "WEB-INF/score/temp";
    }

    /**
     * 查询讲师所带的班级及课程
     * @param session
     * @return
     */
    @RequestMapping("findByEmpId")
    @ResponseBody
    public Map findByEmpId(HttpSession session){
        User1 user=(User1)session.getAttribute("user");
        Integer uid = user.getUid();
        List<Teachercourse1> list=teacherCourseService.findByTeacherId(uid);
        Map<String,Object> map=new HashMap<>();
        map.put("data", list);
        map.put("msg", "");
        map.put("code", 0);
        map.put("count",list.size());
        return map;
    }

    @RequestMapping("findClassCourse")
    @ResponseBody
    public List findClassCourse(Integer classId){
        Teachercourse teachercourse=new Teachercourse();
        teachercourse.setClassId(classId);
        List<Teachercourse1> byExample = teacherCourseService.findByExample(teachercourse);
        return byExample;
    }

    /**
     * 查询并返回班级所有学生，准备输入成绩
     * */
    @RequestMapping("findStudent")
    @ResponseBody
    public Map findStudent(Integer classId,Integer courseId,HttpSession session){
        System.out.println(classId+"++++"+courseId);
        Map<String,Object> map=new HashMap<>();
//        查出课程信息
        Course one = courseService.findOne(courseId);
//        查出班级的每一个学生
        Student student=new Student();
        student.setClassId(classId);
        List<Student1> all = studentService.findAll(student);
        for (Student1 e : all) {
            Score1 score=new Score1();
            Student student1=new Student();
            student1.setStudentId(e.getStudentId());
            student1.setStudentName(e.getStudentName());
            score.setStudentId(student1);
            score.setClassId(e.getClassId());
            score.setCourseId(one);
//            判断该学生信息是否添加进成绩表
            List<Score1> byExample = scoreService.findByExample(e.getStudentId(), courseId);
//            如果没有则添加学生信息
            if (byExample.size()==0) {
                add(score, session);
            }
        }
        List<Score1> list=scoreService.findByClassAndCourse(classId,courseId);
        map.put("data", list);
        map.put("msg", "");
        map.put("code", 0);
        map.put("count",list.size());
        return map;
    }

    /**
     * 修改学生成绩
     */
    @RequestMapping("update")
    @ResponseBody
    public String updateById(Score score) {
        System.out.println(score);
        try {
            scoreService.updateById(score);
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
        return "ok";
    }

    /**
     * 学生查询自己成绩
     */
    @RequestMapping("findStuScore")
    @ResponseBody
    public Map<String, Object> findStuScore(HttpSession session){
        List<Score1> stuScore = null;
        try {
            User1 user =(User1) session.getAttribute("user");
            stuScore = scoreService.findStuScore(user.getUid());
        } catch (Exception e) {
            e.printStackTrace();
        }
        for (Score1 e : stuScore) {
            System.out.println(e);
        }
        Map<String,Object> map=new HashMap<>();
        map.put("data", stuScore);
        map.put("msg", "");
        map.put("code", 0);
        map.put("count", stuScore.size());
        return map;
    }

    /**
     * 讲师查询学生成绩
     */
    @RequestMapping("findJsScore")
    @ResponseBody
    public Map findJsScore(FindStudentUtile studentUtile, HttpSession session,Integer page,Integer limit) {
        Map<String,Object> map= null;
        try {
            map = new HashMap<>();
            User1 user=(User1) session.getAttribute("user");
            PageHelper.startPage(page,limit);
            List<Score1> jsScore = scoreService.findJsScore(user.getUid(), studentUtile);
            PageInfo<Score1> pageInfo=new PageInfo<>(jsScore);
            map.put("data", pageInfo.getList());
            map.put("msg", "");
            map.put("code", 0);
            map.put("count", pageInfo.getTotal());
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
        return map;
    }
   /**
    * 班主任查询成绩
    * */
    @RequestMapping("findBzrScore")
    @ResponseBody
    public Map<String, Object> findBzrScore(FindStudentUtile studentUtile,Integer page,Integer limit, HttpSession session) {
        User1 user=(User1) session.getAttribute("user");
        PageHelper.startPage(page,limit);
        List<Score1> jsScore = scoreService.findBzrScore(user.getUid(), studentUtile);
        PageInfo<Score1> pageInfo=new PageInfo<>(jsScore);
        Map<String,Object> map=new HashMap<>();
        map.put("data", pageInfo.getList());
        map.put("msg", "");
        map.put("code", 0);
        map.put("count", pageInfo.getTotal());
        return map;
    }


    /**
     * 就业专员查成绩
     */
    @RequestMapping("findJyzyScore")
    @ResponseBody
    public Map<String, Object> findJyzyScore(FindStudentUtile studentUtile,Integer page,Integer limit) {
        PageHelper.startPage(page,limit);
        List<Score1> jsScore = scoreService.findJyzyScore(studentUtile);
        PageInfo<Score1> pageInfo=new PageInfo<>(jsScore);
        Map<String,Object> map=new HashMap<>();
        map.put("data", pageInfo.getList());
        map.put("msg", "");
        map.put("code", 0);
        map.put("count", pageInfo.getTotal());
        return map;
    }
}
